package com.how.warlock.tilemap;

import java.util.Random;

import com.how.warlock.Game;
import com.how.warlock.engine.Sprite;
import com.how.warlock.player.Camera;
import com.how.warlock.art.Art;

public class TileMap {
	private byte[][] data;
	
	private int sizex, sizey;
	
	private Sprite[] sprites;
	
	public final static int tileSize = 32;
	
	public TileMap(int sizex, int sizey){
		data = new byte[sizex][sizey];
		this.sizex = sizex;
		this.sizey = sizey;
		randomize();
		
		sprites = new Sprite[Art.tiles.length];
		for(int i = 0 ; i < Art.tiles.length; i++){
			sprites[i] = new Sprite(Art.tiles[i]);
		}
	}
	
	public boolean checkCollision(int x, int y){
		System.out.println(x/32 + "  " + y/32);
		if (x/32 >= 0 && x/32 < sizex && y/32>=0 && y/32<sizey){
			return data[x/32][y/32] == 2;
		} else return false;
	}
	
	public void draw(Game game){
		int minx = Camera.getX()/32 - 1;
		int miny = Camera.getY()/32 - 1;
		int maxx = sizex + Camera.getX()/32 + 1;
		int maxy = sizey + Camera.getY()/32 - ((game.getHeight()/2)/32) + 1;
		
		for (int i = minx; i < maxx; i++){
			for (int j = miny; j < maxy; j++){
				if( i >= 0 && i < sizex && j >= 0 && j < sizey) sprites[data[i][j]].draw(i*tileSize - Camera.getX(), j * tileSize - Camera.getY());
			}
		}
	}
	
	private void randomize(){
		Random rand = new Random();
		for (int i = 0; i < sizex; i++){
			for (int j = 0; j < sizey; j++){
				int r = rand.nextInt(20);
				if (r == 0) data[i][j] = 2; else data[i][j] = 0;
			}
		}
	}
}
